<?php

namespace app\adminapi\controller\v1\exam;

use app\adminapi\controller\AuthController;
use app\exceptions\AdminException;
use app\exceptions\AuthException;
use app\services\exam\ExamRecordService;
use app\services\exam\ExamSendService;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Vod\V20180717\Models\SearchMediaRequest;
use TencentCloud\Vod\V20180717\VodClient;
use think\facade\App;

class ExamRecord extends AuthController
{
    public function __construct(App $app, ExamRecordService $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    public function index()
    {
        $where = $this->request->getMore([
            ['website_id', $this->website],
            ['page', 1],
            ['pageSize', 20],
            ['exam_id', ''],
            ['idcard', ''],
            ['user_name', ''],
            ['exam_type', ''],
            ['read_status', ''],
            ['submit_type', ''],
        ]);
        return app()->make('json')->successful($this->services->index($where));
    }

    public function createOrUpdate()
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['status', 0],
//            ['title',''],
//            ['user_id',''],
//            ['class_id',''],
//            ['major_id',''],
//            ['level',''],
//            ['idcard',''],
//            ['start_face',''],
//            ['middle_face',''],
//            ['end_face',''],
//            ['exam_id',''],
            ['submit_type', ''],
//            ['exam_type',''],
//            ['content',''],
//            ['total_score',''],
//            ['robot_score',''],
//            ['school_score',''],
//            ['pass_score',''],
//            ['start_time',''],
//            ['end_time',''],
//            ['examStart_time',''],
//            ['examEnd_time',''],
            ['website_id', $this->website],
        ]);
        return app()->make('json')->successful($this->services->createOrUpdate($where));
    }

    public function delete()
    {
        $where = $this->request->getMore([
            ['id', ''],
        ]);
        if (!$where['id']) throw new AuthException('缺少必要参数');
        return app()->make('json')->successful($this->services->delete($where));
    }


    /**
     * 批阅试卷
     * @return void
     */
    public function MarkingPapers()
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['robot_score', ''],
            ['school_score', ''],
            ['content', ''],
            ['read_status', '已批阅'],
        ]);
        return app()->make('json')->successful($this->services->MarkingPapers($where));

    }


    /**
     * 查询监控回放视频
     * @return void
     */
    public function selectVideUrl()
    {
        $where = $this->request->getMore([
            ['idcard', ''],
            ['exam_id', ''],//考试批次

        ]);
        $cred = new Credential("AKIDd1tYv28VsynLlwDCuDD4eeZEExCNjPKs", "bwBByVmob8RTFtTosiLfkaDaiZIo0xgR");
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("vod.tencentcloudapi.com");

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new VodClient($cred, "", $clientProfile);

        $req = new SearchMediaRequest();

        $params = [
            "StreamIds" => [$where['idcard'] . $where['exam_id']]
        ];
        $req->fromJsonString(json_encode($params));

        $resp = $client->SearchMedia($req);
        if (empty($resp->MediaInfoSet)) throw new AdminException('没找到相关视频！');
        return app()->make('json')->successful($resp->MediaInfoSet[0]->BasicInfo->MediaUrl);
    }

}